package steak.xteamy.VoiceChat;

import de.maxhenkel.voicechat.api.VoicechatServerApi;
import de.maxhenkel.voicechat.api.opus.OpusDecoder;
import de.maxhenkel.voicechat.api.opus.OpusEncoder;

public class AudioAnonymizer {
    private static final VoicechatServerApi api = Plugin.voicechatServerApi;

    public static byte[] audioProcessor(byte[] data) {
        OpusDecoder opusDecoder = api.createDecoder();
        OpusEncoder opusEncoder = api.createEncoder();

        short[] shorts = opusDecoder.decode(data);
        short[] increasedData = increaseVolume(shorts, 1.1f); //处理示例：音量更改
        return opusEncoder.encode(increasedData);
    }

    private static short[] increaseVolume(short[] data, float factor) {
        short[] result = new short[data.length];

        for (int i = 0; i < data.length; i++) {
            int increasedValue = (int) (data[i] * factor);
            //int increasedValue = (int) (Math.sin(i * 0.1) * factor) + data[i];

            if (increasedValue > Short.MAX_VALUE) {
                increasedValue = Short.MAX_VALUE;
            } else if (increasedValue < Short.MIN_VALUE) {
                increasedValue = Short.MIN_VALUE;
            }

            result[i] = (short) increasedValue;
        }

        return result;
    }
}